# -*- coding: utf-8 -*-
import sys
import warnings
warnings.filterwarnings('ignore')
sys.path.append('../..')
from common.config import *
from db.mysql import MysqlClient
from itertools import product
import requests,execjs,re,json
from urllib.parse import  urlencode
from common.helper import Helper


class Spider(object):
    def __init__(self):
        self.mysql = MysqlClient()
        self.name = "女性安康疾病保险"
    # 生成产品数据
    def genProductInfo(self):
        plan = {
            "planA":{
                # 年龄段
                "year": ["16-35","36-49","50-56"],
                # 性别
                "gender":["M"],
                # 保障期限
                "expire":["1年"],
                #保障金额
                "money":["30000"],
                #交费类型
                "payType":["年交"],
                #计划
                "plan":["A"]
            },
            "planB":{
                # 年龄段
                "year": ["16-35", "36-49", "50-56"],
                # 性别
                "gender": ["M"],
                # 保障期限
                "expire": ["1年"],
                # 保障金额
                "money": ["50000"],
                # 交费类型
                "payType": ["年交"],
                # 计划
                "plan": ["B"]

            },
            "planC":{
                # 年龄段
                "year": ["16-35", "36-49", "50-56"],
                # 性别
                "gender": ["M"],
                # 保障期限
                "expire": ["1年"],
                # 保障金额
                "money": ["80000"],
                # 交费类型
                "payType": ["年交"],
                # 计划
                "plan": ["C"]

            },
            "planD":{
                # 年龄段
                "year": ["16-35", "36-49", "50-56"],
                # 性别
                "gender": ["M"],
                # 保障期限
                "expire": ["1年"],
                # 保障金额
                "money": ["100000"],
                # 交费类型
                "payType": ["年交"],
                # 计划
                "plan": ["D"]
            }
        }
        for k in plan:
            for v in product(plan[k]["year"],plan[k]["gender"],plan[k]["expire"],plan[k]["money"],plan[k]["payType"],plan[k]["plan"]):
                self.mysql.cursor.execute( domain[self.name]["insert_sql"].format(
                    v[0],v[1],v[2],v[3],v[4],v[5]
                ))
                self.mysql._conn.commit()
                print( domain[self.name]["insert_sql"].format(
                    v[0],v[1],v[2],v[3],v[4],v[5]
                ))
    #执行爬虫
    def requestJson(self,dt=None):
        dt =self.mysql.randTask( self.name )
        print( dt )

        data = {

            "insuranceid_25445_保险金额": "50000",
            "suiteId": "5862",
            "period": "365",
            "amount":"",
            "personCount": "1",
            "backInsuranceCount":"",
            "youngAccident": "womanAccident"
        }
        payload = urlencode( data ).replace("+","")
        r = requests.post( url = domain[self.name]["url"],headers=domain[self.name]["header"],data=payload)
        if r.status_code == 200 and r.json()["matchingRuleResult"]:
            price = Helper.decimalParse( r.json()["totalPrice"] )
